package gamagora.game
{
	import Box2D.Dynamics.b2World;
	
	import gamagora.box2d.OSPhysicWorld;
	import gamagora.game.map.OSWorldMap;
	import gamagora.game.utils.OSFPSTimer;
	
	import org.flixel.FlxBasic;
	import org.flixel.FlxCamera;
	import org.flixel.FlxG;
	import org.flixel.FlxSprite;
	import org.flixel.FlxState;
	import org.flixel.FlxText;
	import org.flixel.FlxU;
	
	public class OSPlayState extends FlxState
	{	
		//Infos
		private var _fpsTimer:OSFPSTimer;
		private var _fps:FlxText;
		private var _nbPhyObj:FlxText;
		private var _nbFlxObj:FlxText;
		private var _currentMap:FlxText;
		
		//World
		private var _worldMap:OSWorldMap;
		
		public override function create():void
		{
			//World
			OSPhysicWorld.create(30, 1 / FlxG.framerate);
			OSPhysicWorld.addContactListener(new OSContactListener());
			_worldMap = new OSWorldMap();
			add(_worldMap);
			
			//Infos 
			_fpsTimer = new OSFPSTimer();
			_fps = new FlxText(5, 5, 200).setFormat(null, 16, 0xffffff, "left", 0x233e58);
			_fps.scrollFactor.x = _fps.scrollFactor.y = 0;
			add(_fps);
			
			_nbPhyObj = new FlxText(5, 25, 200).setFormat(null, 16, 0xffffff, "left", 0x233e58);
			_nbPhyObj.scrollFactor.x = _nbPhyObj.scrollFactor.y = 0;
			add(_nbPhyObj);
			
			_nbFlxObj = new FlxText(5, 45, 200).setFormat(null, 16, 0xffffff, "left", 0x233e58);
			_nbFlxObj.scrollFactor.x = _nbFlxObj.scrollFactor.y = 0;
			add(_nbFlxObj);
			
			_currentMap = new FlxText(5, 65, 200).setFormat(null, 16, 0xffffff, "left", 0x233e58);
			_currentMap.scrollFactor.x = _currentMap.scrollFactor.y = 0;
			add(_currentMap);
		}
		
		public override function update():void
		{
			//Mise à jour des éléments du monde (cartes)
			_worldMap.update();
			
			//Mise à jour de la simulation physique
			OSPhysicWorld.update();
			
			//Mise à jour des éléments graphiques
			super.update();
			
			//Infos
			_fpsTimer.update();
			_fps.text = _fpsTimer.fps.toFixed(1) + " fps";
			_nbPhyObj.text = OSPhysicWorld.getNbObjects() + " bodies";
			_nbFlxObj.text = _worldMap.nbSprites + " sprites";
			_currentMap.text = "cm = " + _worldMap.currentMap;
		}
		
	}
}
